package com.zzw.paymentdemo.service.impl;

import com.google.gson.Gson;
import com.zzw.paymentdemo.entity.PaymentInfo;
import com.zzw.paymentdemo.enums.PayType;
import com.zzw.paymentdemo.mapper.PaymentInfoMapper;
import com.zzw.paymentdemo.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     *记录支付日志
     * @param plainText
     **/
    @Override
    public void createPaymentInfo(String plainText) {
//        明文转换Map
        Gson gson=new Gson();
        Map<String,Object> plainTextMap = gson.fromJson(plainText, Map.class);
//        订单号
        String orderNo = (String)plainTextMap.get("out_trade_no");
//        支付订单号
        String transactionId = (String)plainTextMap.get("transaction_id");
//        交易类型
        String tradeType = (String)plainTextMap.get("trade_type");
//        交易状态
        String tradeState = (String)plainTextMap.get("trade_state");
//        订单金额信息
        Map<String,Object> amount = (Map)plainTextMap.get("amount");
//        用户支付金额
        Integer payerTotal = ((Double) amount.get("payer_total")).intValue();

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentType(PayType.WXPAY.getType());
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setTradeType(tradeType);
        paymentInfo.setTradeState(tradeState);
        paymentInfo.setPayerTotal(payerTotal);
        paymentInfo.setContent(plainText);

        baseMapper.insert(paymentInfo);

    }
}
